package com.ocom.accesservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.accesservice.dao.IDaoAttendanceRules;
import com.ocom.accesservice.service.AttendanceRulesService;
import com.ocom.common.entity.access.EntityAttendanceRules;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.request.access.GetAttendanceRulesInfoReq;
import com.ocom.common.utils.DateUtil;
import com.ocom.mysql.aspect.DataSourceAspect;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class AttendanceRulesServiceImpl extends ServiceImpl<IDaoAttendanceRules, EntityAttendanceRules> implements AttendanceRulesService {


    @Override
    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.SLAVE)
    public List<EntityAttendanceRules> getTaskList(Long companyId,Integer type) {
        //30分钟前
        if(type==1){
            String startTime  = DateUtil.getStrDate(new Date(),"HH:mm");
            String checkTime = DateUtil.getStrDate(new Date(System.currentTimeMillis()-30*60*1000),"HH:mm");
            LambdaQueryWrapper<EntityAttendanceRules> queryWrapper=new LambdaQueryWrapper<>();
            //当前时间要大于等于   任务时间 要大于（当前-30分钟）
            queryWrapper.eq(EntityAttendanceRules::getTimeType,1);
//            queryWrapper.le(EntityAttendanceRules::getTaskTime,startTime);
//            queryWrapper.ge(EntityAttendanceRules::getTaskTime,checkTime);
            return this.list(queryWrapper);
        }else{
            String startTime  = DateUtil.getStrDate(new Date(),"HH:mm");
            String checkTime = DateUtil.getStrDate(new Date(System.currentTimeMillis()-30*60*1000),"HH:mm");
            LambdaQueryWrapper<EntityAttendanceRules> queryWrapper=new LambdaQueryWrapper<>();
            //当前时间要大于等于   任务时间 要大于（当前-30分钟）
            queryWrapper.eq(EntityAttendanceRules::getTimeType,2);
//            queryWrapper.le(EntityAttendanceRules::getEndTime,startTime);
//            queryWrapper.ge(EntityAttendanceRules::getEndTime,checkTime);
            return this.list(queryWrapper);
        }
    }

    @Override
    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.SLAVE)
    public List<EntityAttendanceRules> getAttendanceRulesList(Long companyId, GetAttendanceRulesInfoReq request) {
        LambdaQueryWrapper<EntityAttendanceRules> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(EntityAttendanceRules::getComId,request.getComId());
        queryWrapper.apply("FIND_IN_SET({0}, area_ids) > 0",request.getAreaId());
        queryWrapper.eq(EntityAttendanceRules::getZoneNo,request.getZoneNo());
        return this.list(queryWrapper);
    }
}
